﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravelAgencyDAL;
using Beginning;

namespace BusTravelAgency
{
    public partial class Logon : Form
    {
        private Employee currentUser;
        public Employee CurrentUser
        {
            get { return currentUser; }
            set { currentUser = value; }
        }
        
        public Logon()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Employee currentUser = DAO.Verification(txtUser.Text, txtPassword.Text);
            if (currentUser == null)
            {
                MessageBox.Show("User Name or Password is not valid!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                switch (currentUser.RoleID)
                {
                    case 1:
                        //Application.Run(new frmUser());
                        break;
                    case 2:
                        frmMain mainForm = new frmMain();
                        mainForm.Show();
                        this.Hide();
                        break;
                    case 3:
                        //Application.Run(new frmReservations());
                        break;
                    default:
                        MessageBox.Show("This User is not permited!", "Warning", MessageBoxButtons.OK,                                                MessageBoxIcon.Warning);
                        break;
                }
                this.DialogResult = DialogResult.OK;
            }
        }

        public Employee GetUser()
        {
            if (this.DialogResult == DialogResult.OK)
                return currentUser;
            else
                return null;
        }

        private void txtUser_Validating(object sender, CancelEventArgs e)
        {
            ValidateUserName();
        }

        private bool ValidateUserName()
        {
            if (IsUserNameValid(txtUser.Text))
            {
                epValidateData.SetError(txtUser, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtUser, "Musn't be empty!");
                return true;
            }
        }

        private bool IsUserNameValid(string userName)
        {
            return !string.IsNullOrEmpty(userName);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
